﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using VDataAdapter.BlockingQueue;
using VDataAdapter.EventManagement;

using log4net;

namespace VDataAdapter
{
    class PSAdapterEventCapturer : PSAdapterThread
    {
        protected EventManager _simulator = new EventManager();

        protected readonly ILog _log = LogManager.GetLogger("PSAdapterEventCapturer");

        private int _seconds;

        /// <summary>
        /// Start a thread that will run for 'seconds' and record all events down
        /// </summary>
        /// <param name="resetEvent"></param>
        /// <param name="qMain"></param>
        /// <param name="seconds"></param>
        public PSAdapterEventCapturer(ManualResetEvent resetEvent, CommunicationDataQueue qMain, int seconds)
            : base(resetEvent, qMain)
        {
            _seconds = seconds;
            // start a new thread
            // this thread wait for sometimes
            // and then write all event happens into disk
            // at the same time, write all str as well
            Thread th = new Thread(EndCapture);
            th.Start();

        }

        protected void EndCapture()
        {
            for (int i = 0; i < _seconds; i++)
            {
                Thread.Sleep(1000);
            }

            PriceServerObjectFile writer = new PriceServerObjectFile();
            writer.WriteToFile("messageoutput.test", this.StrQueue.ToArray());
            _simulator.WriteEvents();
        }

        protected override void GetFirstTime(IDictionary<string, PriceLibrary.PriceService.StockBase> baseprices,
            IDictionary<string, PriceLibrary.PriceService.StockBase> top3Prices)
        {
           
                //_log.Info("GetFirstTime: " + baseprices.Count.ToString());
                _simulator.AddFirstTimeEvent(baseprices, top3Prices);
                base.GetFirstTime(baseprices, top3Prices);
      
        }

        protected override void Top3PricesStockChanged(IDictionary<string, PriceLibrary.PriceService.StockBase> top3Prices)
        {
            //_log.Info("Top3Prices: " + top3Prices.Count);
            _simulator.AddTop3PricesEvent(top3Prices);
            base.Top3PricesStockChanged(top3Prices);
        }

        protected override void MarketInfo_Changed(IDictionary<int, PriceLibrary.PriceService.StockBase> marketInfos)
        {
            //_log.Info("MarketInfo: " + marketInfos.Count);
            _simulator.AddMarketInfoChangedEvent(marketInfos);
            base.MarketInfo_Changed(marketInfos);
        }

        protected override void ProcessPtTopPrices(IList<PriceLibrary.PriceService.StockBase> newstockbase)
        {
            //_log.Info("PtTopPrices: " + newstockbase.Count);
            _simulator.AddPutThroughChangedEvent(newstockbase);
            base.ProcessPtTopPrices(newstockbase);
        }

        protected override void ProcessPtTransaction(IList<PriceLibrary.PriceService.StockBase> transactions)
        {
            //_log.Info("PtTransaction: " + transactions.Count);
            _simulator.AddPutThroughTransactionEvent(transactions);
            base.ProcessPtTransaction(transactions);
        }

        protected override void ReceivedAllTransaction(IList<PriceLibrary.ChartService.Transaction> transactions, int countNumber, int totalCountNumber)
        {
            //_log.Info("All Transaction: " + transactions.Count + " count: " + countNumber.ToString() + " total: " + totalCountNumber.ToString());
            _simulator.AddTransactionsEvent(transactions, countNumber, totalCountNumber);
            base.ReceivedAllTransaction(transactions, countNumber, totalCountNumber);
        }
        //        _PriceCallBack.Top3PriceChanged += Top3PricesStockChanged;
        //_PriceCallBack.EndReceivedNewTickers += NewTickersReceived;
        //_PriceCallBack.MarketInfoChanged += MarketInfo_Changed;
        //_PriceCallBack.EndReceivedPtTopPrices += PtTopPrices_Changed;
        //_PriceCallBack.EndReceivedPtTransactions += PtTransactions_Changed;
    }
}
